package cn.edu.hunau.oop.day04.demo;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author hb
 * @date 2025-08-07 11:16:32
 */
public class TestUserSort {


    @Test
    public void defaultSort(){
        User[] users = initData();

        Arrays.sort(users);
        System.out.println(Arrays.toString(users));

    }

    @Test
    public  void customSort(){
        User[] users = initData();
        Arrays.sort(users,new SortByStringLength());
        System.out.println(Arrays.toString(users));
    }


    @Test
    public  void customSort2(){
        User[] users = initData();
        //利用匿名内部提供了Comparator的实现类的匿名对象
        Arrays.sort(users,new Comparator<User>(){
            @Override
            public int compare(User o1, User o2) {
                return o1.getName().length() - o2.getName().length();
            }
        });
        System.out.println(Arrays.toString(users));
    }

    @Test
    public  void customSort3(){
        User[] users = initData();
        //利用匿名内部提供了Comparator的实现类的匿名对象
        Arrays.sort(users, (o1,o2)->o1.getName().length() - o2.getName().length());
        System.out.println(Arrays.toString(users));
    }

    @Test

  public void test01(){
      String[] arr={"C++","Java","Web","Spring Boot","Python"};


      Arrays.sort(arr,(String s1,String s2)->{ return s1.length()-s2.length();});
      System.out.println(Arrays.toString(arr));
  }

  @Test
  public void test02(){
        Integer[] arr={23,34,3,54,4,32,234,2,2134,3245};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        Arrays.sort(arr,(a,b)->b-a);
        System.out.println(Arrays.toString(arr));
  }


    public User[] initData(){
        User[] users = new User[5];
        users[0] = new User(1001,"Lucy");
        users[1] = new User(22,"zhang san");
        users[2] = new User(312,"wangwu");
        users[3] = new User(4123,"lisi");
        users[4] = new User(512,"tanseng");

        return users;
    }
}


class SortByStringLength implements Comparator<User>{
    @Override
    public int compare(User o1, User o2) {
        //排序规则:根据用户的姓名的长度进行排序
        return o1.getName().length() - o2.getName().length();
    }
}